package com.sdjz.club.club.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdjz.club.club.entity.Images;
import com.sdjz.club.club.mapper.ImagesMapper;
import com.sdjz.club.club.utils.ImageUtil;
import com.sdjz.club.club.utils.SnowFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/club/images")
public class ImagesController {

    @Autowired
    ImagesMapper imagesMapper;

    @PostMapping("/insert")
    public CommonResult insert(
            @RequestParam("getId") Long getId,
            @RequestParam("getType") String getType,
            @RequestParam("img") MultipartFile img
    ) {

        Long id = SnowFlow.getId();
        Images images = new Images();
        images.setGetType(getType);
        images.setGetId(getId);
        images.setImageId(id);
        try {
            ImageUtil.updateImg(img, id);
            imagesMapper.insert(images);
        } catch (IOException e) {
            return CommonResult.fail("储存图片失败");
        }
        return CommonResult.succ();
    }

    @GetMapping("/select")
    public CommonResult select(
            @RequestParam("getId") Long getId,
            @RequestParam("getType") String getType
    ) {
        return CommonResult.succ(imagesMapper.selectList(
                        new QueryWrapper<Images>()
                                .eq("get_id", getId)
                                .eq("get_type", getType)
                )
        );
    }

}

